require('lua.util.StringUtils')

local map = {}

local function genCID()
	local cid
	repeat
		cid = math.random(-0xffffffff, 0xffffffff)
	until not map[cid]
	map[cid] = true
	return cid
end

--- @class Class
--- @field cid number
local Class = {}
Class.cid = genCID()

--- @param other Class
--- @return boolean
function Class:instanceof(other)
	local status, ret = pcall(function()
		if self.cid == other.cid then
			return true
		end
		return self.__extends:instanceof(other)
	end)
	return status and ret
end

--- @param other Class
--- @return boolean
function Class:equals(other)
	return self == other
end

--- @return string
function Class:toString()
	return 'Class(' .. self.cid .. ')'
end

--- @generic T
--- @param template T
--- @return T
return function(template)
	template.new = template.new or function()
		return error('Class:new() is not implemented.')
	end
	template.cid = genCID()
	template.__index = template
	return setmetatable(template, {
		__index = template.__extends or Class
	})
end